package com.example.zpositionanimationdemo.slice;

import com.example.zpositionanimationdemo.Ability.*;
import com.example.zpositionanimationdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.window.service.WindowManager;

public class MainAbilitySlice extends AbilitySlice {
    private Button z_Position_but, xy_Position_but, z_Position_Animation_but, Shadow_Styles_but, Programmatically_Add_but;
    private Text textTitle;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        initData();
        OnClickListener();
    }

    private void initView() {
        z_Position_but = (Button) findComponentById(ResourceTable.Id_Z_POSITION_but);
        xy_Position_but = (Button) findComponentById(ResourceTable.Id_XY_POSITION_but);
        z_Position_Animation_but = (Button) findComponentById(ResourceTable.Id_Z_POSITION_ANIMATION_but);
        Shadow_Styles_but = (Button) findComponentById(ResourceTable.Id_SHADOW_STYLES_but);
        Programmatically_Add_but = (Button) findComponentById(ResourceTable.Id_PROGRAMMATICALLY_ADD_but);
        textTitle = (Text) findComponentById(ResourceTable.Id_text_title);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(-13615201);
    }

    private void initData() {}

    private void OnClickListener() {
        z_Position_but.setClickedListener(component -> {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    nextPage(ZPosition .class);
                }
            }, 300);
        });

        xy_Position_but.setClickedListener(component -> {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    nextPage(XyPosition.class);
                }
            }, 300);
        });
        z_Position_Animation_but.setClickedListener(component -> {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    nextPage(ZPositionAnimation.class);
                }
            }, 300);
        });
        Shadow_Styles_but.setClickedListener(component -> {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    nextPage(ShadowStyles.class);
                }
            }, 300);
        });
        Programmatically_Add_but.setClickedListener(component -> {
            getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                @Override
                public void run() {
                    nextPage(ProgrammaticallyAdd.class);
                }
            }, 300);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void nextPage(Class nextPage) {
        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.zpositionanimationdemo")
                .withAbilityName(nextPage.getName())
                .build();
        secondIntent.setOperation(operation);
        // 通过AbilitySlice的startAbility接口实现启动另一个页面
        startAbility(secondIntent);
    }
}
